home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 513 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.9 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Correctness of compilers behavior
  5. Date: 18 Feb 1996 02:14:25 GMT
  6. Organization: Comp Sci, University of Melbourne
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4g3i93$9lp@mulga.cs.mu.OZ.AU>
  9. References: <3122157A.2EB2@orbotech.co.il>
  10. NNTP-Posting-Host: taumet.eng.sun.com
  11. Content-Type: text
  12. X-Nntp-Posting-Host: munta.cs.mu.oz.au
  13. Content-Length: 2150
  14. Originator: clamage@taumet
  15.  
  16. "Constantine Antonovich:" <const@Orbotech.Co.IL> writes:
  17.  
  18. >//     Could somebody  tell if the following code is
  19. >// ill-formed
  20.  
  21. The code is well-formed, but has undefined behaviour.
  22. So you should not be surprised if you get different
  23. results using different compilers.
  24.  
  25. >#include <iostream.h>
  26. >#include <assert.h>
  27. >#include <new.h>
  28. >
  29. >class A {
  30. >public:
  31. >  A(void)     { cout << "A constructed" << endl; }
  32. >  A(const A&) { cout << "A copied" << endl; }
  33. >  ~A()        { cout << "A destructed" << endl; }
  34. >};
  35. >
  36. >class B {
  37. >public:
  38. >  B(void)     { cout << "B constructed" << endl; }
  39. >  B(const B&) { cout << "B copied" << endl; }
  40. >  ~B()        { cout << "B destructed" << endl; }
  41. >};
  42. >
  43. >A* foo(unsigned size)
  44. >{
  45. >  assert(sizeof(A)==sizeof(B));
  46.  
  47. This assertion is not guaranteed to succeed.  It would take an
  48. extremely perverse implementation for it to fail, however, so I think it
  49. would be very portable, even though it is not strictly guaranteed to work.
  50.  
  51. >  B* bptr=new B[size];
  52. >  A* aptr=(A*)bptr;
  53.  
  54. This cast has unspecified behavior.  (See 5.2.9 [expr.cast.reinterpret]/8.)
  55. However, I would expect it to work on most implementations.
  56.  
  57. >  for (unsigned i=0; i<size; ++i) {
  58. >    (bptr+i)->~B();
  59.  
  60. That's fine.
  61.  
  62. >    new(aptr+i) A;
  63.  
  64. Assuming that `(void *)(aptr+i)' is correctly aligned and points to an
  65. areas of sufficient space (which as noted above is not guaranteed, but
  66. is likely to hold for most implementations) this is OK.
  67.  
  68. >  }
  69. >
  70. >  return aptr;
  71. >}
  72. >
  73. >int main(void)
  74. >{
  75. >  A* arr=foo(2);
  76.  
  77. With the caveats listed above, this is likely to be OK on most implementations.
  78.  
  79. >  delete [] arr;
  80.  
  81. This has undefined behaviour.  It contravenes 5.3.5[expr.delete]/2, which
  82. says that the expression passed to `delete []' must be a pointer to the
  83. first element of an array of objects allocated with `new []'; this is not
  84. the case, because although there once was such an array at that memory
  85. location, its lifetime ended when the memory was overwritten by the calls
  86. to placement new (see 3.8[basic.life]/1).
  87.  
  88. >  return 0;
  89. >}
  90.  
  91. --
  92. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  93. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  94.  
  95. [ To submit articles: Try just posting with your newsreader.  If that fails,
  96.               use mailto:std-c++@ncar.ucar.edu
  97.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  98.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  99.   Comments? mailto:std-c++-request@ncar.ucar.edu
  100. ]
  101.